package com.example.demo.controller;

import java.util.Date;
import java.util.List;


import com.example.demo.entity.Cart;
import com.example.demo.service.ICartService;
import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * cartController
 * 
 * @author ruoyi
 * @date 2025-04-24
 */
@RestController
@RequestMapping("/system/cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    /**
     * 查询cart列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Cart cart , @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "12")int pageSize)
    {
        PageResult pageResult = cartService.CartGoodsList(cart,pageNum,pageSize);
        return ResponseResult.createBySuccess(200,pageResult,"查询成功");
    }


    /**
     * 导出cart列表
     */


//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Cart cart)
//    {
//        List<Cart> list = cartService.selectCartList(cart);
//        ExcelUtil<Cart> util = new ExcelUtil<Cart>(Cart.class);
//        util.exportExcel(response, list, "cart数据");
//    }

    /**
     * 获取cart详细信息
     */

    @GetMapping(value = "/{cartid}")
    public ResponseResult getInfo(@PathVariable("cartid") Long cartid)
    {
       Cart cart  = cartService.selectCartByCartid(cartid);
        return  ResponseResult.createBySuccess(200,cart,"查询成功");
    }

    /**
     * 新增cart
     */
    @PassToken
    @PostMapping
    public ResponseResult add(@RequestBody Cart cart)
    {

        Cart ci = new Cart();
        BeanUtils.copyProperties(cart, ci); // 属性拷贝：从 cart 复制到 ci

        Cart c = cart;
        c.setPrice(null);
        c.setQuantity(null);
        List<Cart> list = cartService.selectCartList(c);

        if (list.size() != 0) {
            cart.setUpdateDate(new Date());
            int i = cartService.updateCartNum(ci); // 修改数据
            if (i > 0) {
                return ResponseResult.createBySuccess( 200, "",  "修改成功");
            } else {
                return ResponseResult.createByFail(500, "修改失败");
            }
        } else {
            int i = cartService.insertCart(ci);
            if (i > 0) {
                return ResponseResult.createBySuccess( 200, "",  "修改成功");
            } else {
                return ResponseResult.createByFail(500, "添加失败");
            }
        }
    }


    /**
     * 修改cart
     */
    @PassToken
    @PutMapping
    public ResponseResult edit(@RequestBody Cart cart)
    {
        System.out.println("123");
        int i = cartService.updateCart(cart);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","修改成功");
        }else {
            return ResponseResult.createByFail(200,"修改失败");
        }
    }

    /**
     * 删除cart
     */
    @PassToken
	@DeleteMapping("/{cartids}")
    public ResponseResult remove(@PathVariable Long[] cartids)

    {
        System.out.println(123);
        int i = cartService.deleteCartByCartids(cartids);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","修改成功");
        }else {
            return ResponseResult.createByFail(200,"修改失败");
        }
    }
}
